package com.getpebble.android.jskit.bridge;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.appmessage.AppMessage;
import com.getpebble.android.framework.appmessage.AppMessage.AppMessageCommand;
import com.getpebble.android.framework.appmessage.JsonPebbleDictionary;
import com.getpebble.android.framework.appmessage.PebbleDictionary;
import com.getpebble.android.jskit.webapps.JsKit;
import java.io.Serializable;
import java.util.UUID;
import org.json.JSONException;

public class AppMessageBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        JsAppContextBridge.getInstance().setAppContext(context);
        if (intent != null && intent.getAction() != null) {
            String action = intent.getAction();
            int transactionId = intent.getIntExtra("transaction_id", -1);
            UUID appUuid = null;
            Serializable serializableExtraUuid = intent.getSerializableExtra("uuid");
            if (serializableExtraUuid != null) {
                if (serializableExtraUuid instanceof UUID) {
                    try {
                        appUuid = (UUID) serializableExtraUuid;
                    } catch (Exception e) {
                        Trace.error("AppMessageBroadcastReceiver", "unable to parse UUID");
                    }
                } else if (serializableExtraUuid instanceof String) {
                    try {
                        appUuid = UUID.fromString((String) serializableExtraUuid);
                    } catch (Exception e2) {
                        Trace.error("AppMessageBroadcastReceiver", "unable to parse UUID");
                    }
                }
            }
            PebbleDictionary dictionary = null;
            AppMessageCommand command = null;
            if ("com.getpebble.action.app.RECEIVE_ACK".equals(action)) {
                command = AppMessageCommand.ACK;
            } else if ("com.getpebble.action.app.RECEIVE_NACK".equals(action)) {
                command = AppMessageCommand.NACK;
            } else if ("com.getpebble.action.app.RECEIVE".equals(action)) {
                command = AppMessageCommand.PUSH;
                try {
                    dictionary = JsonPebbleDictionary.fromJson(intent.getStringExtra("msg_data"));
                } catch (JSONException e3) {
                    Trace.error("AppMessageBroadcastReceiver", "unable to parse JSON in appmessage", e3);
                }
            }
            JsKit.jsKitAccess().onAppMessageReceivedEvent(new AppMessageReceivedEvent(new AppMessage((byte) (transactionId & 255), appUuid, command, dictionary)));
        }
    }
}
